package eva.savant.business.material.view

import eva.savant.core.view.Maestro
import eva.savant.businessobject.mt.Centro
import eva.savant.businessobject.mt.Almacen
import eva.savant.businessobject.mt.Almacenero
import eva.savant.businessobject.mt.AlmacenDetalle

import javax.faces.bean.ManagedBean
import javax.faces.bean.SessionScoped
import javax.annotation.PostConstruct
import javax.faces.convert.Converter

@SessionScoped
@ManagedBean(name = 'IMAlmacen')
public class IMAlmacen extends Maestro {

  def lsCentro

  Boolean stock

  AlmacenDetalle detail

  @PostConstruct
  public void cleanEntity() {

    entity= new Almacen(); init()

    entity.centro= new Centro()

    entity.lsAlmacenDetalle= new ArrayList<AlmacenDetalle>()


    detail= new AlmacenDetalle()

    detail.almacenero= new Almacenero()


    stock= true

    lsCentro= find(Centro.class,'')
  }

  public void editEntity(Object id) {

    entity= (Almacen) get(entity.class, id)

    stock= entity.stock == 1
  }

  public boolean mergeEntity(Object entity) {

    entity.stock = stock? 1 : 0

    return merge(entity) != null
  }

  public void detailAdd() {

    detailDelete(detail.almacenero.id)

    entity.lsAlmacenDetalle.add new AlmacenDetalle(almacen:     entity,
                                                   almacenero:  detail.almacenero,
                                                   posicion:    detail.posicion,
                                                   operacionId: ALTA,
                                                   sociedadId:  sociedad.id,
                                                   usuarioId:   usuario.id)

    detail= new AlmacenDetalle()

    detail.almacenero= new Almacenero()
  }

  public void detailDelete(almaceneroId) {

    for(x in entity.lsAlmacenDetalle)

    if(x.almacenero.id.equals(almaceneroId)) { entity.lsAlmacenDetalle.remove(x); break }
  }

  public List complete(String value) { return completeLike(Almacenero.class,'usuario.persona.nombre',value) }

  public Converter getConverter() { return converterLike(Almacenero.class) }
}
